export default class extends think.Service {
    constructor() {
        super();
        this.MDictModel = this.db['sys_mdict'];
    }

    /**
     * 根据搜索条件查询字典列表
     */
    async queryDictBySeach(data) {
        let conditionStr = '';
        if (data.status) {
            conditionStr += ` and sys_mdict.status=${data.status}`
        } else {
            conditionStr += ` and sys_mdict.status!=1`
        }
        if (data.tree_name) {
            conditionStr += ` and sys_mdict.tree_name like '%${data.tree_name}%'`
        }
        let sql = `select sys_mdict.*,DATE_FORMAT(sys_mdict.create_date,'%Y-%m-%d %h:%i:%s') create_date,
                    DATE_FORMAT(sys_mdict.update_date,'%Y-%m-%d %h:%i:%s') update_date,create_user.realname as create_realname,
                    update_user.realname as update_realname from sys_mdict 
                   left join sys_user create_user on create_user.id=sys_mdict.create_by
                   left join sys_user update_user on update_user.id=sys_mdict.update_by where 1=1 ${conditionStr}`
        let list = await this.CrudService.querySql(sql);
        if (list.length > 0) {
            return await new this.TreeService().transData(list);
        } else {
            return list;
        }
    }


    /**
     * 查询字典列表
     */
    async queryDictList(data) {
        let conditionStr = '';

        let list = await this.MDictModel.findAll();
        return list;
    }

    /**
     * 保存多级字典
     */
    async saveMDict(data) {
        try {
            let parent = await this.MDictModel.findOne({where: {id: data.parent_id}, row: true});
            data.parent_ids = '';
            data.tree_names = '';
            data.level = '';
            data.create_date = new Date();
            data.update_date = new Date();
            if (parent === null) {
                parent = {
                    id: 0,
                    parent_ids: '',
                    tree_names: '',
                    level: 0,
                    tree_name: '',
                    sort: 0,
                    parent_id: ''
                }
            }
            return await new this.TreeService().saveTree('sys_mdict', parent, data)
        } catch (error) {
            return this.exceptionService.handleError(error);
        }
    }

    /**
     * 修改多级字典
     */
    async updateMDict(data) {
        try {
            let parent = await this.MDictModel.findOne({where: {id: data.parent_id}, row: true});
            data.parent_ids = '';
            data.tree_names = '';
            data.level = '';
            data.create_date = new Date();
            data.update_date = new Date();
            return new this.TreeService().saveTree('sys_mdict', parent, data)
        } catch (error) {
            return this.exceptionService.handleError(error);
        }

    }

    /**
     * 根据id删除多级字典及其所有字节点，
     */
    async deleteMDict(id) {
        try {
            const Op = this.db.sequelize.Op;
            return new this.TreeService().deleteParentAndChildrenNode('sys_mdict', id);
        } catch (error) {
            return this.exceptionService.handleError(error);
        }
    }

    /**
     * 根据id删除多级字典及其所有字节点，
     */
    async updateDictStatus(id, status) {
        try {

            return this.MDictModel.update({status: status}, {where: {id: id}})
        } catch (error) {
            return this.exceptionService.handleError(error);
        }
    }
}
